#include <stdlib.h>
#include <stdio.h>
#include "common_func.h"
#include <termios.h>
#include <unistd.h>


int mygetch(void)
{
  struct termios oldt, newt;
  int ch;
  tcgetattr( STDIN_FILENO, &oldt );
  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  ch = getchar();
  tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
  return ch;
}


long microsecsonds_since_midnight_GMT(void)
{
  struct timeval tvs;
  gettimeofday(&tvs, NULL);
  return tvs.tv_usec;
}

//function to ensure if this process is runn
int is_running()
{
    char *lockfile = "/tmp/acnClient.lock" ;
    char cmd[128];
    FILE* fp = fopen(lockfile, "r");
    if (fp != NULL)
    {
    	fprintf(stderr, "ERROR: this process have opened : %s!\n", lockfile);
    	fclose(fp);
        return 1;
    }
    sprintf(cmd, "touch %s",lockfile);
    system(cmd);
    return 0;
}
//function to ensure if this process is runn
int close_running(){
	char *lockfile = "/tmp/acnClient.lock" ;
	char cmd[128];
	sprintf(cmd, "r %s",lockfile);
	system(cmd);
	FILE* fp = fopen(lockfile, "r");
	if (fp == NULL)
	{
		return 1;
	} else{
		return 0;
	}

}

